<?php

/**
 * IndexController - Index
 *
 * @author Fernando Dias Oliveira <rodox17@gmail.com>
 * @package intranet
 * @subpackage controller
 */

class PedidoController extends AppBaseController 
{
	public function init()
	{
		parent::init();		
		
		$this->_helper->layout->disableLayout();
	}

	
	function salvarNomeAction()
	{
		$caf02 = new caf02();
		$data = $this->_request->getPost();
		
		$caf02->update(array("caf02_nome_cliente" => $data["caf02_nome_cliente"]), "caf02_cod = {$data["cod"]}");
	}
	
	function mostrarEnderecoAction(){
		$caf02=new caf02();
		$cod=$_POST["cod"];
		
		$nome=$caf02->find($cod)->current()->toArray();
		
		die($nome["caf02_endereco"]);
	}
	
	function salvarEnderecoAction(){
		$caf02=new caf02();
		$cod=$_POST["cod"];
		$nome=$_POST["nome"];
		
		$caf02->update(array("caf02_endereco"=>$nome),"caf02_cod=$cod");
		
		exit;
	}
	
	public function ajaxSalvarPagamentoAction()
	{
		$caf11 = new caf11();
		$caf12 = new caf12();
		
		$json = $this->_request->getPost("json");
		$json = json_decode("[$json]");
		
		$data = array();
		$dataPagamento = array();
		
		for ($i = 0; $i < sizeof($json); $i++)
		{
			$data["caf11_tipo_pagamento"] = $json[$i]->tipo_pagamento;
			$data["caf11_valor"] =  str_replace(",", ".", $json[$i]->valor_pago);
			
			$idPagamento = $caf11->salvar($data);
			
			$dataPagamento["caf12_cod_pagamento"] = $idPagamento;
			$dataPagamento["caf12_cod_ordem"] = $json[$i]->ordem;
			
			$caf12->salvar($dataPagamento);
		}
				
		exit;
	}
	
	public function setcookielistAction()
	{
		$tipo = $this->_request->getPost("tipo");
		$stat = $this->_request->getPost("stat");
		
		
		
		$arr = explode("|", $_COOKIE["tipo"]);
		$cook = $_COOKIE["tipo"] . "|" . $tipo;
		
		if($stat)
		{
			if(strstr($_COOKIE["tipo"], "$tipo|"))
			{
				$cook = str_replace("$tipo|", "", $_COOKIE["tipo"]);
			}
			else
			{
				$cook = str_replace("|$tipo", "", $_COOKIE["tipo"]);
			}
			
			setcookie("tipo", $cook);
		}
		
		if(array_search($tipo, $arr))
		{
			exit;
		}
		
		setcookie("tipo", $cook);
		
		exit;
	}
	
	public function getcookielistAction()
	{
		//unset($_COOKIE["tipo"]);
		//die;
		$arr = array();
		
		foreach(explode("|", $_COOKIE["tipo"]) as $key => $val)
		{
			if($val)
			{
				$arr[] = $val;
			}
		}
		
		echo json_encode($arr);
		
		//echo json_encode($arr);
		
		exit;
	}
	
	public function savecomentAction()
	{
		$caf04 = new caf04();
		
		$data = $this->_request->getPost();
		
		foreach ($data["prod_radio"] as $k => $v)
		{
			$data["caf04_cod"] = $v;
			$data["caf04_coment"] = ucfirst(strtolower($data["caf04_coment"]));
			
			if($this->_request->getPost("all"))
			{
				//$caf04->saveComentAll($_POST);
			}
			else
			{
				$caf04->saveComent($data);
			}
		}
		
		exit;
	}
	
	public function savedescontoAction()
	{
		$caf04 = new caf04();
		$caf02 = new caf02();
		
		if($this->_request->getPost("all"))
		{
			$caf02->saveDesconto($_POST);
		}
		else
		{
			
			
			//$_POST["desconto"] = ;
			$caf04->saveDesconto($_POST);
		}
		//die(str_replace($_POST["desconto"], ",", "."));
		exit;
	}

    public function indexAction() 
    {


    }
    
    public function newAction()
    {
		$caf02 = new caf02();
		
		$this->view->mesas = $caf02->getAll();
    }
    
    public function pedidoAction()
    {
    	$caf02 = new caf02();
   		$caf03 = new caf03();
   		
   		$str = "";
   		
   		foreach($caf02->getPedidoByMesa($this->_request->getPost("cod_mesa")) as $key => $val)
   		{
   			$str .= $val["prod"] . ",";
   		}
		
   		$this->view->itens_str = substr($str, 0, strlen($str) - 1);
   		
		$arr = $caf03->getArray();
		
		$this->view->arr_menu = $arr;
		
		$this->view->pedido = $this->_request->getPost("cod_mesa");
		$this->view->arr_ped = $caf02->getAllById($this->_request->getPost("cod_mesa"));
    }
    
    public function disactiveAction()
    {
    	$caf02 = new caf02();
    	
    	$caf02->disactivePedido($_POST["cod_pedido"], $_POST["cod_usuario"], $_POST["forma_pagamento"]);
    	
    	exit;
    }
    
    public function listallAction()
    {

    }
    
    public function listallFecharAction()
    {

    }
    
    public function listtodosAction()
    {
    	$caf04 = new caf04();
    	
		$total_ped = array();
		$cozinha = array();
		$bar = array();
		$cod_ped = "";
		$tot = "";
		$takeaway = false;
		$normal = false;
		
		foreach ($caf04->getAllAvulcos() as $key => $val)
		{
			if($val["caf02_mesa"])
			{
				continue;
			}
					
			if($val["caf02_cod"] == $cod_ped)
			{
				$tot = $total_ped[$val["caf02_cod"]]["total"] + $val["caf05_valor"];
				$total_ped[$val["caf02_cod"]] = array("cod" => $val["caf02_cod"], "total" => $tot, "data" => $val["caf02_date"].$val["caf02_time"], "caf02_nome_cliente" => $val["caf02_nome_cliente"], "user" => $val["caf01_user"], "mesa" => $val["caf02_mesa"]);
			}
			else
			{
				$total_ped[$val["caf02_cod"]] = array("cod" => $val["caf02_cod"], "total" => $val["caf05_valor"], "data" => $val["caf02_date"].$val["caf02_time"], "caf02_nome_cliente" => $val["caf02_nome_cliente"], "user" => $val["caf01_user"], "mesa" => $val["caf02_mesa"]);
			}
			
			if($total_ped[$val["caf02_cod"]]["mesa"] == 0)
			{
				$takeaway = true;
			}
			
			if($total_ped[$val["caf02_cod"]]["mesa"] != 0)
			{
				$normal = true;
			}
			
			/**
			 * Cuzinha.
			 */
			
			if($val["caf03_cod"] == 1)
			{
				/*if($ult_cozinha == $val["caf05_cod"])
				{
					if($val["caf04_coment"])
					{
						$coment_coz .= "1 " . $val["caf04_coment"] . "; ";
					}
					*/
				
				$mesa = $val["caf02_mesa"];
				
					if(!$val["caf02_mesa"])
					{
						$mesa = "TKW";
					}
					
					
					$cozinha[] = array("entregue" => Data::getHora($val["caf04_entregue"]), "cod_prod" => $val["caf04_cod"], "cod" => $val["caf02_cod"], "data" => Data::getHora($val["caf02_date"].$val["caf02_time"]), "mesa" => $mesa, "car" => $val["caf05_car"], "prod" => $val["caf05_prod"], "coment" => $val["caf04_coment"]);
				//}
				/*else
				{
					$coment_coz = "";
					
					$cozinha[$val["caf05_cod"]] = array("mesa" => $val["caf02_mesa"], "car" => $val["caf05_car"], "prod" => $val["caf05_prod"], "qtd" => 1, "coment" => $val["caf04_coment"]);
				}
				
				$ult_cozinha = $val["caf05_cod"];*/
			}
			
			/**
			 * Bar.
			 */
			
			if($val["caf03_cod"] == 2)
			{
				/*if($ult_bar == $val["caf05_cod"])
				{
					if($val["caf04_coment"])
					{
						$coment_bar .= "1 " . $val["caf04_coment"] . "; ";
					}
					*/
				$mesa = $val["caf02_mesa"];
				
					if(!$val["caf02_mesa"])
					{
						$mesa = "TKW";
					}
				
					$bar[] = array("entregue" => Data::getHora($val["caf04_entregue"]), "cod_prod" => $val["caf04_cod"], "cod" => $val["caf02_cod"], "data" => Data::getHora($val["caf02_date"].$val["caf02_time"]), "mesa" => $mesa, "car" => $val["caf05_car"], "prod" => $val["caf05_prod"], "coment" => $val["caf04_coment"]);
				}
				/*else
				{
					$coment_bar = "";
					
					$bar[$val["caf05_cod"]] = array("mesa" => $val["caf02_mesa"], "car" => $val["caf05_car"], "prod" => $val["caf05_prod"], "qtd" => 1, "coment" => $val["caf04_coment"]);
				}*/
				
				//$ult_bar = $val["caf05_cod"];
			//}
			
			$cod_ped = $val["caf02_cod"];
		}
		
		array_multisort($cozinha);
		array_multisort($bar);
		
		//Zend_Debug::dump($caf04->getAllTakeaway());

		$this->view->takeaway = $takeaway;
		$this->view->total_ped = $caf04->getAllTakeaway();
		$this->view->normal = $normal;
		$this->view->cozinha = $cozinha;
		$this->view->bar = $bar;
    }
    
    public function listarTodosItensAction()
    {
    	$caf04 = new caf04();
    	
		$arr = $caf04->fetchTodasMesasTodosItens();
		
		$this->view->total_ped = $arr;
		
		//Zend_Debug::dump($arr);exit;
    }
    
    public function excluirPedidoAction(){
    	$caf02=new caf02();
    	$caf02->update(array("caf02_active"=>0),"caf02_cod=".$_POST["cod"]);
    	
    	exit;
    }
    
    public function setentregueAction()
    {
    	$caf04 = new caf04();
    	
    	$cod = $this->_request->getPost("cod");
    	
    	$caf04->setEntregue($cod);

    	exit;
    }
    
    public function formasPagamentoAction()
    {
    	$caf04 = new caf04();
    	$mesa  = $this->_request->getPost("numeroMesa");
    	
    	$this->view->total = $caf04->getTotalPorMesa($mesa);
    }
    
    public function ajaxlistallAction()
    {
    	$caf04 = new caf04();
    	
    	$this->view->arr_pedido = $caf04->getAll();
    	$this->view->userId     = $this->_request->getPost("user_id"); 	
    	$this->view->gerente    = $this->_request->getPost("gerente"); 	
    }
    
    public function ajaxlistallFecharAction()
    {
    	$caf04 = new caf04();
    	
    	$this->view->arr_pedido = $caf04->getAll();
    	$this->view->userId     = $this->_request->getPost("user_id"); 	
    	$this->view->gerente    = $this->_request->getPost("gerente"); 	
    }
    
    public function listAction()
    {
    	$caf04 = new caf04();
    	$caf02 = new caf02();
    	
    	if($mesa = $this->_request->getPost("mesa"))
    	{
    		$arr = $caf02->getLastByMesa($mesa);
    		
    		if(!$arr)
    		{
    			die("<font color='red'>Nenhum pedido.</font>");
    		}
    	}
    	else 
    	{
	    	$arr = $caf02->getAllById($this->_request->getPost("cod"));
    	}
    	
    	$auxArr = array();
    	$total = array();
    	$cod = 0;
    	$prod = "";
    	$pr = 1;
    	$sing = false;
    	$qtdCom = 1;
    	
    	if(!is_array($arr))
    	{
    		$arr = array();
    	}
    	
    	if($menu = $this->_request->getPost("menu"))
    	{
    		$arr_prod = $caf04->getProductsByCodMenu($this->_request->getPost("cod"), $menu);
    	}
    	else
    	{
    		$cod_pedi = $this->_request->getPost("cod");
    		
    		if($mesa)
    		{
    			$cod_pedi = $arr[0]["caf02_cod"];
    		}
    		
    		$arr_prod = $caf04->getProductsByCodPedido($cod_pedi);
    	}
    	
    	foreach ($arr_prod as $key => $val)
    	{
   			if($prod == $val["caf04_cod_prod"] && $val["caf04_cod_prod"])
    		{
    			if($val["caf04_coment"])
    			{
    				if($coment == $val["caf04_coment"])
    				{
    					$qtdCom++;
    				}
    				else 
    				{
    					$qtd = 1;
    					
    					$qtdCom = 1;
    					
    					$cod++;
    				}
    				
    				if($qtdCom)
    				{
    					$qtd = $qtdCom;
    				}
    				
    				$val["caf04_qtd"] = $qtd;
    			}
    			else
    			{
    				//$sing = true;
    				
					$val["caf04_qtd"] = $pr + 1;
    			}
				
				$auxArr[$cod - $pr] = $val;
    			
				$pr++;
				
    		}
    		else
    		{
    			$qtdCom = 1;
    			
    			$auxArr[$cod] = $val;
    			
    			$pr = 1;
    		}
    		
    		$cod++;
    		
    		$prod = $val["caf04_cod_prod"];
    		$coment = $val["caf04_coment"];
    	}
    	
    	
    	
    	$this->view->arr_pedido = $auxArr;
    	$this->view->res_ped = $arr;
    }
    
    public function ajaxvermesaAction()
    {
    	$caf02 = new caf02();
    	
    	if($caf02->getMesa($this->_request->getPost("mesa")))
    	{
    		echo 1;
    	}
    	
    	exit;
    }
    
    public function saveqtdAction()
    {
    	$caf04 = new caf04();
    	
    	$caf04->saveUn($_POST["cod_menu"], $_POST["cod_item"], $_POST["cod"], $_POST["un"]);
    	
    	exit;
    	
    	//$caf04->
    }
    
    function deleteItemAction(){
    	$caf04 = new caf04();
    	$id    = $this->_request->getPost("id");
    	
    	$caf04->delete("caf04_cod = $id");
    	
    	exit;
    }
    
    public function deleteqtdAction()
    {
    	$caf04 = new caf04();
    	
    	$caf04->deleteUn($_POST["cod"], $_POST["cod_item"]);
    	
    	exit;
    }
    
    public function deletesAction()
    {
    	$caf02 = new caf02();
    	$caf04 = new caf04();
    	
    	$caf02->delete("caf02_cod = {$this->_request->getPost("cod_pedido")}");
    	$caf04->delete("caf04_cod_item = {$this->_request->getPost("cod_pedido")}");
    	
    	exit;
    }
    
    public function saveAction()
    {
    	$cab02 = new caf02();
    	
    	echo $cab02->save($_POST);
    	
    	exit;
    }
    
    public function jsonpassAction()
    {
    	$caf01 = new caf01();
    	
    	//print_r($caf01->getUserByPass($_POST["pass"]));
    	$arr = $caf01->getUserByPass($_GET["pass"]);
    	
    	if(is_array($arr))
    	{    	
    		echo json_encode($arr);
    	}
    	else
    	{
    		echo json_encode(array());
    	}
    	
    	exit;
    }
}

?>